package day11;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author Xiao
 * @date 2021/8/2
 */
public class Test07 {
    /**
     * 7. 键盘录入一个任意字符串，统计字符串中相邻字符出现的次数，将结果写到D盘下的count.txt文件中（面试题）
     * 例如：aaccaabaab
     * count.txt：2a2c2a1b2a1b
     */
    @SuppressWarnings("all")
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String s = sc.nextLine();
//        String s = "aaccaabaab";
        FileOutputStream fos = new FileOutputStream("homework\\sr\\day11\\count.txt");
//        method1(s);
//        mehtod2(s, fos);
        char[] chars = s.toCharArray();
        Character key = null;
        int count = 0;
        for (char ch : chars) {
            if (key == null) {
                key = ch;
            }
            if (ch == key) {
                count++;
            } else {
//                System.out.println(key + "  " + count);
                fos.write(String.valueOf(count).getBytes());
                fos.write(String.valueOf(key).getBytes());
                key = ch;
                count = 1;
            }
        }
//        System.out.println(key + "  " + count);
        fos.write(String.valueOf(count).getBytes());
        fos.write(String.valueOf(key).getBytes());
        fos.close();
//        method3();
    }

    /**老师讲的方法*/
    private static void method3() {
        /**
         * 思路：
         *      1.遍历字符串，获取单个字符
         *      2.由这个字符去找下一个字符判断是否相同
         *          如果相同，就继续找下一个；
         *          如果不同，就停下来。
         *              count：j - i；
         *              i = j；  //下一个要找的元素从j开始
         */
        String s = "aaccaabaab";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length();) {
            char chi = s.charAt(i);
            int j = i + 1;
            while (j < s.length()) {
                char chj = s.charAt(j);
                if (chj != chi) {
                    break;
                }
                j++;
            }
            int count = j - i;
            sb.append(count).append(chi);
            i = j;  //条件控制语句
        }
        System.out.println(sb);
    }

    /**失败版本2*/
    private static void mehtod2(String s, FileOutputStream fos) throws IOException {
        ArrayList<Character> listChar = new ArrayList<>();
        ArrayList<Integer> listInt = new ArrayList<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            int count = 1;
            if (i < chars.length - 1 && chars[i + 1] != chars[i]) {
                listChar.add(chars[i]);
                listInt.add(count);
            } else {
                for (int j = i + 1; j < chars.length; j++) {
                    if (ch == chars[j]) {
                        count++;
                    } else {
                            listChar.add(chars[i]);
                            listInt.add(count);
                            break;
                    }
                }
            }
        }
        if (chars[chars.length - 1] != chars[chars.length - 2]) {
            listChar.add(chars[chars.length - 1]);
            listInt.add(1);
        }
        System.out.println(listChar);
        System.out.println(listInt);
        for (Integer i : listInt) {
            for (Character ch : listChar) {
                byte[] bytes1 = String.valueOf(i).getBytes();
                byte[] bytes2 = String.valueOf(ch).getBytes();
                fos.write(bytes1);
                fos.write(bytes2);
            }
        }
        fos.close();
    }

    /**失败版本1*/
    private static void method1(String s) {
        HashMap<Character, Integer> hm = new HashMap<>();
        char[] chars = s.toCharArray();
        for (char ch : chars) {
            if (hm.keySet().equals(ch)) {
                int count = hm.get(ch);
                hm.put(ch, count + 1);
            } else {
                hm.put(ch, 1);
            }
        }
    }
}
